********************************************************************************
*********** This program is written for the present value project **************
********************************************************************************
* 04. Factor Analysis  
********************************************************************************
* Note: The goal of this program is to analyze factor returns, so we take the 
*   simple approach of using the portfolio-level VW average of log variables.
*   In contrast, Program 05 uses the portfolio-level variables consistent  
*   with the portfolio-level identity.


********************************************************************************
* 04.00  Initialize STATA
********************************************************************************
clear all
set more off, perm

********************************************************************************
* 04.01  Settings
********************************************************************************

* Sample period 
local start = 1965
local end = 2022

* MB requirement 
local mb_req = 0  

* rho
local rho = 0.96

********************************************************************************
* 04.02  Use CRSP to compute returns and merge with Annual Data to compute BM
********************************************************************************

* Load dataset

use "Data/crsp/Downloaded/all crsp monthly 1926-2022", clear
ren *, lower

* Only use common stocks from three major exchanges

keep if inlist(exchcd,1,2,3) & inlist(shrcd,10,11)	

* Add up different distributions (codes missing divamt as zero)

collapse (sum) divamt, by(permno date exchcd siccd dlstcd dlret prc ret ///
	shrout cfacshr cfacpr)

* Year-month identifier t
	
gen t = mofd(date)									
format t %tm
g month = month(date)

* Compute Market Cap (shrout in thousands; express ME in millions)			     
																				 
replace prc = abs(prc)															 
gen ME = prc * shrout * 10^(-3)		
bys permno (t): g ME_L1 = ME[_n-1] if t==t[_n-1]+1	
bys permno (t): g ME_L12 = ME[_n-12] if t==t[_n-12]+12	 										 

* Code missing returns as zero unless it's the firm's first observation

replace ret = . if inlist(ret,-66,-77,-88,-99)
sort permno t
replace ret = 0 if mi(ret) & permno==permno[_n-1] 

* Delisting return adjustment following Shumway (1997) & Shumway and Warther (2002) (Note FF don't do this; but the code doesn't make a big difference.)

replace dlret = . if inlist(dlret,-55,-66,-88,-99)
replace dlret = cond(dlstcd==500|(dlstcd>=520)*(dlstcd<=584), ///
		cond(inlist(exchcd,1,2),-.35,-.55),.) if mi(dlret) 
replace dlret = -1 if dlret < -1												 
replace ret = (1+ret)*(1+dlret)-1 if ~mi(dlret)									 
replace ret = dlret if mi(ret) 

/* Annualize Dividends (Jul t - June t+1) */

* Annualize returns	
															
g R = ret 

* Price and shares outstanding adjusted for stock splits

g P = prc/cfacpr
g N = shrout * cfacshr

* Compute backward looking return from previous June 

g monthsfromjune = cond(month >= 7, month - 6, month + 6)
forval i = 1/11 {
	qui bys permno (t): replace R = (1+R)*(1+ret[_n-`i'])-1 if ///
		t==t[_n-`i']+`i' & `i' <= monthsfromjune - 1
	}
	
replace R = . if month[_n-monthsfromjune]!=6 | /// 								
	permno!=permno[_n-monthsfromjune] 	

* Compute implied dividend from price changes and returns

bys permno (t): gen D = (1 + R) * P[_n-monthsfromjune] - P if ///
	t <= t[_n-1] + 12

* Filter out rounding errors in cfacpr (up to 0.2% rounding error in cfacpr)

qui bys permno (t): replace D = 0 if abs(D/P[_n-monthsfromjune]) < .002 ///
	| D < 0
	
* Total dividends (at the firm-level)

bys permno (t): g Dtotal = D * N[_n-monthsfromjune] * 10^(-3) 

* Diluted shares

bys permno (t): g N_dilute = N[_n-monthsfromjune]/N  

* If new issue, set N_dilute to be zero

bys permno (t): g new = 1 if _n == 1 & ~mi(ME)
replace N_dilute = 0 if new == 1 & mi(N_dilute)	
replace Dtotal = 0 if new == 1 & mi(Dtotal) 
drop new

* Set year to merge with CCM 

gen year = year(date)
gen myear = cond(month >= 1 & month <= 6, year - 1, year)   

tempfile crsp
save "`crsp'"
				
********************************************************************************
* 04.03  Generate characteristics
********************************************************************************

use "Data/compustat/Downloaded/all ccm annual 1950-2021", clear

* Rename variables

ren *, lower
ren lpermno permno

* Convert fiscal year of calendar year and keep the latest fiscal year-end observation

gen myear = cond(fyr <= 5, fyear + 2, fyear + 1)
bys permno myear (fyr): keep if _n == _N

* Recode variables

recode cogs xint xsga (.=0) if cogs <. | xint <. | xsga <.
recode txditc (.=0)

replace txditc = 0 if mi(txditc)
replace seq = at - lt if mi(seq)

* Book equity

gen preferred = pstkrv
replace preferred = pstkl if preferred==.
replace preferred = pstk if preferred==.
replace preferred = 0 if preferred==. 

gen BE = seq + txditc - preferred
replace BE = . if BE <= 0
bys permno (myear): gen BE_L1 = BE[_n-1] if myear==myear[_n-1]+1
 
* Compute investment (book equty growth), profitability (ROE), and others

gen OP = (revt - cogs - xsga - xint)/BE											// Operating profitability of Fama-French
bys permno (myear): gen AG = at/at[_n-1] - 1 if myear==myear[_n-1]+1			// Asset growth of Fama-French
bys permno (myear): gen ROE = ni/BE[_n-1] if myear==myear[_n-1]+1
bys permno (myear): gen Gni = ni/ni[_n-1] if myear==myear[_n-1]+1
bys permno (myear): gen GOP = OP*BE/(OP[_n-1]*BE[_n-1]) if myear==myear[_n-1]+1
g roe = log(1+ROE)

* Define issuer & repurchaser (GH (2012))

bys gvkey (myear): gen NS = log(csho*ajex) - log(csho[_n-1]*ajex[_n-1]) if myear==myear[_n-1]+1
g GHissuer = cond(NS > 0.1 & ~mi(NS), 1, 0)
g GHrep = cond(NS < -0.005 & ~mi(NS), 1, 0)
	
/* Merge scale */ 	

preserve

u "Data/Created/Annual Data 2021 with Bins", clear

if `mb_req' == 1 {
	drop if MB_L1 < 1/100 | MB_L1 > 100 | MB < 1/100 | MB > 100
}


* Keep relevant variables and save data

keep gvkey year SCALE
ren year myear

bys gvkey myear: keep if _n==_N

tempfile iva
save "`iva'"

restore		

merge m:1 gvkey myear using "`iva'", keep(3) nogen

keep permno gvkey datadate myear BE BE_L1 OP AG ROE roe ni SCALE GH* G*

* Save data

compress																		 
sort permno myear
tempfile compustat
save "`compustat'"

																		
********************************************************************************
* 04.04  Generate accounting-based characteristic bins
********************************************************************************

* First, compute the BM characteristic

use "`crsp'", clear

	keep if month == 12																// E.g., 2015/12 ME used to generate BM that lead to portfolio returns in 2016/7-2017/6 (myear 2016)
	replace myear = year(date) + 1 			
								
	merge 1:1 permno myear using "`compustat'", update replace 

	keep if _merge == 3
	drop _merge
	
	* Collapse by GVKEY  (TRY MB BY PERMNO TO CHECK CORRELATION W/ HML)

	collapse (sum) ME (mean) BE, by(gvkey myear)

	* Compute BM

	gen BM = BE / ME
	gen bm = log(BM) 

	* Temp save data

	keep gvkey myear BM bm 
		
	tempfile BM
	save "`BM'"

* Next, compute the size characteristic

use "`crsp'", clear

	keep if month == 6																// E.g., 2016/6 ME used for size sorts on portfolios realized in 2016/7-2017/6 (myear 2016)
	replace myear = year(date) 
	keep permno myear ME exchcd
		
	* Merge other characteristics	

	merge m:1 permno myear using "`compustat'"
		drop if _merge==2
		drop _merge

	* Collapse by GVKEY

	collapse (sum) ME ///
		(mean) BE OP AG ROE roe SCALE (min) exchcd, by(gvkey myear)

	* Merge BM

	merge 1:1 gvkey myear using "`BM'"
		drop if _merge==2
		drop _merge
		
* Apply the MB Requirement (TRY WITHOUT TO CHECK CORRELATION W/ HML)

if `mb_req' == 1 {
	replace BM = . if BM < 1/100 | BM > 100
}

* Form characteristic bins 

gen ffdata = cond(~mi(ME) & ~mi(BM), 1, 0)

	foreach char of varlist ME {
			gsort myear -ffdata exchcd   
			forvalues j = 50/50 {
				by myear: egen p`j' = pctile(`char') if exchcd==1 & ffdata==1, ///
					p(`j')
				replace p`j' = p`j'[_n-1] if myear==myear[_n-1] & mi(p`j') & ///
					ffdata==1
			}
			qui gen bin_`char' = 1 if `char' <= p50 & ~mi(`char') & ~mi(p50)
			qui replace bin_`char' = 2 if (`char' >= p50) & ~mi(`char') & ///
				mi(bin_`char') & ~mi(p50)
			drop p50 
		}	

	foreach char of varlist BM OP AG SCALE{
			gsort myear -ffdata exchcd  
			forvalues j = 30(40)70 {
				by myear: egen p`j' = pctile(`char') if exchcd==1 & ffdata==1, ///
					p(`j')
				replace p`j' = p`j'[_n-1] if myear==myear[_n-1] & mi(p`j') & ///
					ffdata==1
			}
			qui gen bin_`char' = 1 if `char' <= p30 & ~mi(`char') & ~mi(p30)
			qui replace bin_`char' = 2 if (`char' < p70) & ~mi(`char') & ///
				mi(bin_`char') & ~mi(p70)
			qui replace bin_`char' = 3 if (`char' >= p70) & ~mi(`char') & ///
				mi(bin_`char') & ~mi(p70)
			drop p30 p70 
		}	
		
	* Decile portfolios	
		
	foreach char of varlist ME BM OP AG SCALE{
		forvalues j = 10(10)90 {
			gsort myear -ffdata exchcd  
			by myear: egen p`j' = pctile(`char') if exchcd==1 & ffdata==1, p(`j')
			replace p`j' = p`j'[_n-1] if myear==myear[_n-1] & mi(p`j')
			}
		qui g dec_`char' = 1 if `char' <= p10 & ~mi(`char')
		forvalues i = 2/9 {
			local j = 10*`i'
			qui replace dec_`char' = `i' if `char' < p`j' & ~mi(`char') & mi(dec_`char')
			}
		qui replace dec_`char' = 10 if (`char' >= p90) & ~mi(`char') & mi(dec_`char')
		drop p10-p90
		}	
		
	* Quintile portfolios
	
	foreach char of varlist ME BM OP AG SCALE{
		gen quin_`char' = ceil(dec_`char'/2)	
		}
			
	* Change the direction of bin_AG

	replace bin_AG = 4 - bin_AG

	* Save temporarily for merge with crsp

	keep gvkey myear bin* dec* quin* BE BM OP AG bm roe ROE SCALE
		
	tempfile bins
	save "`bins'"

* IVA Inputs

use "`crsp'", clear
	
	keep if month == 6
	replace myear = year(date)
	
	keep permno myear ME ME_L12 Dtotal N_dilute
	foreach var of varlist ME ME_L12 Dtotal N_dilute{
		ren `var' `var'_June
		}
	
	tempfile iva_input
	save `iva_input'
																		
********************************************************************************
* 04.05  Merge crsp and bins
********************************************************************************

use "`crsp'", clear

* Merge Compustat and get GVKEY

merge m:1 permno myear using "`compustat'", keep(3) nogen

* Merge Bins

merge m:1 gvkey myear using "`bins'"
drop if _merge==2
drop _merge

* Merge IVA inputs

merge m:1 permno myear using "`iva_input'", keep(1 3) nogen

* Define firm
	
egen firm = group(permno)
			
********************************************************************************
* 04.06  Compute accounting-based characteristic bins with CRSP returns data
********************************************************************************
	
* BM2: Use up-to-date ME

gen BM2 = BE / ME
gen bm2 = log(BM2)

* Momentum

bys firm (t): gen MOM = 1 + ret[_n-1] if t==t[_n-11]+11 & ///
					firm==firm[_n-11] & ~mi(ret[_n-11]) & ~mi(ret[_n-1])
forval i = 2/11 {
	bys firm (t): replace MOM = MOM * (1+ret[_n-`i'])
}

gen r12m = log(MOM * (1+ret))
bys firm (t): g r12m_L1 = r12m[_n-1] if t==t[_n-1]+1

* Monthly ME and momentum bins

gen ffdata = cond(~mi(ME) & ~mi(BM), 1, 0)

foreach char of varlist ME {
		gsort t -ffdata exchcd   
		forvalues j = 50/50 {
			by t: egen p`j' = pctile(`char') if exchcd==1 & ffdata==1, p(`j')
			replace p`j' = p`j'[_n-1] if t==t[_n-1] & mi(p`j') & ffdata==1
		}
		qui gen bin_`char'2 = 1 if `char' <= p50 & ~mi(`char') & ~mi(p50)
		qui replace bin_`char'2 = 2 if (`char' >= p50) & ~mi(`char') & ///
			mi(bin_`char'2) & ~mi(p50)
		drop p50 
	}	

foreach char of varlist MOM {
		gsort t -ffdata exchcd  
		forvalues j = 30(40)70 {
			by t: egen p`j' = pctile(`char') if exchcd==1 & ffdata==1, p(`j')
			replace p`j' = p`j'[_n-1] if t==t[_n-1] & mi(p`j') & ffdata==1
		}
		qui gen bin_`char' = 1 if `char' <= p30 & ~mi(`char') & ~mi(p30)
		qui replace bin_`char' = 2 if (`char' < p70) & ~mi(`char') & ///
			mi(bin_`char') & ~mi(p70)
		qui replace bin_`char' = 3 if (`char' >= p70) & ~mi(`char') & ///
			mi(bin_`char') & ~mi(p70)
		drop p30 p70 
	}
	
foreach var in bin_ME2 bin_MOM{
	bys firm (t): gen `var'_L1 = `var'[_n-1] if t==t[_n-1]+1
}	
	
* 1- to 5-year return 

g ret1mo = ret
forval i = 1/59 {
	local j = `i'+1
	bys firm (t): gen ret`j'mo = (1+ret`i'mo)*(1+ret[_n+`i'])-1
}	


drop ret2mo-ret11mo ret13mo-ret23mo ret25mo-ret35mo ///
	ret37mo-ret47mo ret49mo-ret59mo
	
* Log scale and clean-surplus ROE

g scale = log(SCALE)
g roeCS = roe + scale
g LR_roeCS = 0	
forval i = 1/5 {
	bys firm (t): gen roeCS_F`i' = roeCS[_n+`i'] if t==t[_n+`i']-`i'
	bys firm (t): replace LR_roeCS = LR_roeCS + `rho'^`i'*roeCS_F`i' if t==t[_n+`i']-`i'
}
	
* Compute value weighted quantities by factor

foreach var of varlist bm roe roeCS LR_roeCS scale ret ret12mo-ret60mo r12m_L1 BM ROE SCALE{
	gen ME_L1_`var' = ME_L1 if ~mi(`var')
	gen ME_L1_times_`var' = ME_L1 * `var'
}


* Save data	
	
save "Data/Created/monthly_panel_dataset_wofilter", replace

********************************************************************************
* 04.07  Compute factors
******************************************************************************** 

foreach factor in BM OP AG SCALE {

	* Import data
	
	use "Data/Created/monthly_panel_dataset_wofilter", clear
	
	* FF data requirement
	
	keep if ffdata == 1
	
	* Portfolio-level identity variables
	
	g L = ME_June*N_dilute_June
	replace ME_June = . if mi(L)
	
	* Collapse by 6 portfolios

	collapse (sum) ME_L1_* BE BE_L1 ME_June ni L Dtotal, by(t bin_`factor' bin_ME)
	
	drop if mi(bin_ME) | mi(bin_`factor')
		
	foreach var in ret r12m_L1 ret12mo ret24mo ret36mo ret48mo ret60mo {
		gen `var' = ME_L1_times_`var' / ME_L1_`var'
		gen `var'_sp = .5*(`var' + `var'[_n-1]) ///
			- .5*(`var'[_n-4] + `var'[_n-5]) if bin_ME==2 & bin_`factor'==3
	} 
		
	forvalues i = 12(12)60 {
		gen r`i'mo_sp = .5*(log(1+ret`i'mo) + log(1+ret`i'mo[_n-1])) ///
			- .5*(log(1+ret`i'mo[_n-4]) + log(1+ret`i'mo[_n-5])) if bin_ME==2 & bin_`factor'==3
	}

	g r1mo = ln(1+ret)
	g r1mo_sp = .5*(r1mo + r1mo[_n-1]) ///
			- .5*(r1mo[_n-4] + r1mo[_n-5]) if bin_ME==2 & bin_`factor'==3
			
	* Portfolio-level identity variables
	
	replace L = L/ME_June
	g bmpf = ln(BE/ME_June)
	g roepf = ln(1 + ni/BE_L1)
	g ivapf = ln((Dtotal + L*BE)/(BE_L1 + ni))
	g roecspf = roepf + ivapf
	
	foreach var of varlist bmpf roepf ivapf roecspf {
		g `var'_sp = .5*(`var' + `var'[_n-1]) ///
			- .5*(`var'[_n-4] + `var'[_n-5]) if bin_ME==2 & bin_`factor'==3
		}
						
	keep if bin_ME==2 & bin_`factor'==3
	keep t ret_sp r1mo_sp ret12mo_sp-ret60mo_sp r12mo_sp-r60mo_sp r12m_L1_sp ///
		bmpf_sp roecspf_sp roepf_sp ivapf_sp

	tempfile tmp`factor'
	save "`tmp`factor''"
}

foreach factor in BM {

	* Import data
	
	use "Data/Created/monthly_panel_dataset_wofilter", clear
		
	* FF data requirement
	
	keep if ffdata == 1
	
	* Collapse by 6 portfolios

	collapse (sum) ME_L1_* , by(t bin_ME bin_`factor')
	
	drop if mi(bin_ME) | mi(bin_`factor')
		
	foreach var in ret r12m_L1 ret12mo {
		gen `var' = ME_L1_times_`var' / ME_L1_`var'
		gen `var'_sp = -(`var' + `var'[_n-1] + `var'[_n-2])/3 ///
			+ (`var'[_n-3] + `var'[_n-4] + `var'[_n-5])/3 if ///
			bin_ME==2 & bin_`factor'==3 
	}   
		
	g r1mo = ln(1+ret)
	g r1mo_sp = -(r1mo + r1mo[_n-1] + r1mo[_n-2])/3 ///
			+ (r1mo[_n-3] + r1mo[_n-4] + r1mo[_n-5])/3 if ///
			bin_ME==2 & bin_`factor'==3 

	keep if bin_ME==2 & bin_`factor'==3
	keep t ret_sp r1mo_sp ret12mo_sp r12m_L1_sp
		
	tempfile tmpME
	save "`tmpME'"
}

foreach factor in MOM {

	* Import data
	
	use "Data/Created/monthly_panel_dataset_wofilter", clear
		
	* FF data requirement
	
	keep if ffdata == 1
	
	* Collapse by 6 portfolios
	
	collapse (sum) ME_L1_*, by(t bin_`factor'_L1 bin_ME2_L1)
	
	drop if mi(bin_ME2_L1) | mi(bin_`factor'_L1)
		
	foreach var in ret r12m_L1 ret12mo {
		gen `var' = ME_L1_times_`var' / ME_L1_`var'
		gen `var'_sp = .5*(`var' + `var'[_n-1]) ///
			- .5*(`var'[_n-4] + `var'[_n-5]) if bin_ME2_L1==2 & ///
			bin_`factor'_L1==3 
	} 
	
	g r1mo = ln(1+ret)
	g r1mo_sp = .5*(r1mo + r1mo[_n-1]) ///
			- .5*(r1mo[_n-4] + r1mo[_n-5]) if bin_ME2_L1==2 & ///
			bin_`factor'_L1==3 
			
	keep if bin_ME2_L1==2 & bin_`factor'_L1==3
	keep t ret_sp r1mo_sp ret12mo_sp r12m_L1_sp
		
	tempfile tmp`factor'
	save "`tmp`factor''"
}

use "`tmpBM'", clear
gen factor = "HML"
append using "`tmpOP'"
replace factor = "RMW" if mi(factor)
append using "`tmpAG'"
replace factor = "CMA" if mi(factor)
append using "`tmpME'"
replace factor = "SMB" if mi(factor)
append using "`tmpMOM'"
replace factor = "MOM" if mi(factor)
append using "`tmpSCALE'"
replace factor = "Scale" if mi(factor)

* Save permanently

keep if t >= ym(1964,7)
save "Data/Created/Factors monthly series", replace

/* Check correlations with FF */

preserve

import excel "Data/ff/F-F_Research_Data_5_Factors_2x3.xlsx", sheet("F-F_Research_Data_5_Factors_2x3") firstrow clear

g t = ym(int(Date/100), mod(Date,100))
format t %tm
drop Date

* Convert %

foreach var of varlist MktRF SMB HML RMW MOM CMA{
	replace `var' = `var'/100
	}
	
tempfile ff
save "`ff'"

restore

merge m:1 t using "`ff'", keep(3) nogen

corr ret_sp HML if factor=="HML"
corr ret_sp MOM if factor=="MOM"
corr ret_sp SMB if factor=="SMB"
corr ret_sp RMW if factor=="RMW"
corr ret_sp CMA if factor=="CMA"


********************************************************************************
* 04.08  HML Predictability Tests
********************************************************************************
* Load additional variables

u "Data/Created/Factors monthly series", clear 
keep if factor == "HML"
	
	* Date setting

	g year = year(dofm(t))
	g month = month(dofm(t))
	g myear = cond(month >= 1 & month <= 6, year - 1, year)   
	replace year = myear
	drop myear
	
	* Save data
	
	save "Data/Created/HML monthly series", replace

local begy = 1965
foreach endy in 2022 {

	u "Data/Created/HML monthly series", clear 

	* Label variables	

	label var bmpf_sp 			"Value spread"
	label var ivapf_sp			"IVA spread"																														
	label var roepf_sp			"Profitability spread"

	* For annual returns, notice that the year variable is the time of the spread,
	* so to get the return in 2020, you use year 2019, etc.
	preserve

	keep if month(dofm(t)) == 7	& t >= ym(`begy',7) & t <= ym(`endy'-1,12) 
	tsset year

	eststo: qui ivreg2 ret12mo_sp bmpf_sp if factor=="HML", robust small bw(2)
	eststo: qui ivreg2 ret12mo_sp bmpf_sp roepf_sp if factor=="HML", robust small bw(2)
	eststo: qui ivreg2 ret12mo_sp bmpf_sp roepf_sp ivapf_sp if factor=="HML", robust small bw(2) 
	
	restore
	
	* Now monthly
	
	tsset t

	eststo: qui ivreg2 ret_sp bmpf_sp if factor=="HML" & t >= ym(`begy',7) & t <= ym(`endy',12), robust small bw(13)
	eststo: qui ivreg2 ret_sp bmpf_sp roepf_sp if factor=="HML" & t >= ym(`begy',7) & t <= ym(`endy',12), robust small bw(13)
	eststo: qui ivreg2 ret_sp bmpf_sp roepf_sp ivapf_sp if factor=="HML" & t >= ym(`begy',7) & t <= ym(`endy',12), robust small bw(13) 
	
	
	* Save the table
	esttab, ar2
 	esttab using "Results/Tables/04_08_`endy'_combined.tex", replace ///
 		t(2) ar2 b(3) nostar drop(_cons) nonotes ///
 		width(\textwidth) label nomtitle ///
 		mgroups("Annual" "Monthly", pattern(1 0 0 1 0 0)  ///
 		prefix(\multicolumn{@span}{c}{) suffix(})   ///
 		span erepeat(\cmidrule(lr){@span})) substitute(\hline\hline \hline)  
		
 	esttab using "Results/Tables/04_08_`endy'_combined_SE.tex", replace ///
 		se(3) ar2 b(3) nostar drop(_cons) nonotes ///
 		width(\textwidth) label nomtitle ///
 		mgroups("Annual" "Monthly", pattern(1 0 0 1 0 0)  ///
 		prefix(\multicolumn{@span}{c}{) suffix(})   ///
 		span erepeat(\cmidrule(lr){@span})) substitute(\hline\hline \hline)  
				
	eststo clear 

}

/* Figures */ 

u "Data/Created/HML monthly series", clear 

local begy = 1965
local endy = 2022

* Set Panel

keep if month(dofm(t)) == 7	& t >= ym(`begy',7) & t <= ym(`endy'-1,12) 
tsset year

* HML Fitted Figures

reg ret12mo_sp bmpf_sp  
	predict fit1
	local r2_fit1: di %04.2f e(r2_a)	
reg ret12mo_sp bmpf_sp roepf_sp 
	predict fit2  
	local r2_fit2: di %04.2f e(r2_a)	
reg ret12mo_sp bmpf_sp roepf_sp ivapf_sp 
	predict fit3  
	local r2_fit3: di %04.2f e(r2_a)

label var ret12mo_sp "HML"
label var fit1 "HML{subscript:VS}"
label var fit2 "HML{subscript:VS,PS}"
label var fit3 "HML{subscript:VS,PS,IS}"

twoway (tsline ret12mo_sp if tin(`begy',`endy'), lcolor(gs8)) ///
  (tsline fit1 if tin(`begy',`endy'), lcolor(blue%80) lpattern(dash)) ///
  (tsline fit3 if tin(`begy',`endy'), lcolor(red) lwidth(medthick)),  ///
	ttitle("") graphregion(color(white)) tlabel(`begy' 1970 1980 1990 ///
	2000 2010 `endy') ylabel(,nogrid) ///
	text(.5 2012.3 "R{superscript:2}{subscript:adj,VS,PS,IS} = `r2_fit3'", size(large) color(red)) ///
	text(.58 2010.5 "R{superscript:2}{subscript:adj,VS} = `r2_fit1'", size(large) color(blue))

graph export "Results/Figures/04_08_HML_fitted_`begy'_`endy'.pdf", replace 


********************************************************************************
* 04.09  IVA factor - FF5F + Mom alphas
********************************************************************************

/* Time-series of quintile portfolios based on policy and lambda */

u "Data/Created/monthly_panel_dataset_wofilter", clear

local start = 1965
local end = 2022

keep if t >= ym(`start',7) & t <= ym(`end',6)

* Rename Scale to accretion

ren SCALE IVA
ren quin_SCALE quin_IVA

* Merge FF factor

preserve

import excel "Data/ff/F-F_Research_Data_5_Factors_2x3.xlsx", sheet("F-F_Research_Data_5_Factors_2x3") firstrow clear

g t = ym(int(Date/100), mod(Date,100))
format t %tm
drop Date

foreach var of varlist MktRF HML SMB CMA RMW MOM RF{
	replace `var' = `var'/100
	}
	
tempfile ff
save "`ff'"

restore

merge m:1 t using "`ff'", keep(3) nogen
sort firm t

* Value-weighted excess return

g Xret = ret-RF

g ME_L1xret = ME_L1*Xret
g ME_L1_Iret = ME_L1 if ~mi(Xret)

/* 1a. Univariate sorts */

mat B_FF6F = J(1,7,.)
mat SE_FF6F = J(1,7,.)

foreach var of varlist IVA{

	preserve
	
	collapse (sum) ME_L1xret ME_L1_Iret, by(t quin_`var')
	
	* Value-weighed excess return

	g ret = ME_L1xret/ME_L1_Iret*100
	drop if mi(quin_`var')
	
	* Reshape data
	
	keep t ret quin_`var'
	reshape wide ret, i(t) j(quin_`var')	
	
	* Long-short
	
	g ret6 = ret5-ret1
	
	* Merge FF
	
	merge 1:1 t using "`ff'", nogen keep(1 3)
	tsset t
	
	* Regressions
	
	forval i = 1/6{
		
		ivreg2 ret`i' MktRF HML SMB CMA RMW MOM, robust small bw(auto)
								
		* Store coefficients and standard errors
		
		mat B_FF6F = (B_FF6F \ (e(b)))
		matrix V = vecdiag(e(V))
		matmap V V, map(sqrt(@))
		matrix SE_FF6F = (SE_FF6F \ V)
					
		}
					
	restore
	
	* Alphas
			
	preserve
	
	clear 
	svmat B_FF6F
	svmat SE_FF6F
	
	keep B_FF6F7 SE_FF6F7
	drop if _n==1
	
	ren B_FF6F7 alpha
	g t = SE_FF6F7
	
	* Reshape data
			
	g pf1 = " $ \alpha $ "
	g pf2 = mod(_n,6)
	replace pf2 = 6 if pf2==0
			
	keep pf1 pf2 alpha t
	reshape wide alpha t, i(pf1) j(pf2)

	* To string alphas
			
	foreach alpha of varlist alpha* {
		qui tostring `alpha', force replace format("%5.3f")
		}
			
	* Put t-stats below alpha
			
	tempfile tmp
	save `tmp'
			
	keep pf1 t*
	forval i = 1/6{
		ren t`i' alpha`i'
		qui tostring alpha`i', force replace format("%5.3f")
		qui replace alpha`i' = "(" + alpha`i' + ")"
		}
	append using `tmp'
	gsort pf1 -t1
	replace pf1 = "\addlinespace" if _n > 1
	drop t*
			
	* Save data
			
	listtab * using "Results/Tables/04_09_`var'.tex", ///
		rstyle(tabular) replace ///
		head("\begin{tabular}{lcccccc}" ///
		"\toprule" ///
		" $ iva $ quintile & 1 & 2 & 3 & 4 & 5 & 5-1 \\" ///
		"\midrule") ///
		foot("\bottomrule" "\end{tabular}")	
					
	restore
	
	}

/* 1b. 5x5 Excess Return */

* Rename variables (to make table look nicer)

ren quin_ME quin_Size

g Size = .

* Compute portfolio excess returns & alphas

mat B_FF6F = J(1,7,.)
mat SE_FF6F = J(1,7,.)
set more off
preserve

import excel "Data/ff/F-F_Research_Data_5_Factors_2x3.xlsx", sheet("F-F_Research_Data_5_Factors_2x3") firstrow clear

g t = ym(int(Date/100), mod(Date,100))
format t %tm
drop Date

foreach var of varlist MktRF HML SMB CMA RMW MOM RF{
	replace `var' = `var'/100
	}
	
tempfile ff
save `ff'

restore
local list Size BM OP
local list2 IVA

foreach var1 in `list'{
	foreach var2 in `list2'{

			preserve

			collapse (sum) ME_L1xret ME_L1_Iret, by(t quin_`var1' quin_`var2')
			
			* Value-weighed excess return

			g ret = ME_L1xret/ME_L1_Iret*100
			drop if mi(quin_`var1') | mi(quin_`var2')
			
			* Assign portfolio coordinates and reshape data
			
			g no = 10*quin_`var1' + quin_`var2'
			
			keep t ret no
			reshape wide ret, i(t) j(no)
			tsset t
			
			* Merge FF data
			
			merge 1:1 t using `ff', nogen keep(1 3)
			
			* Long-short return
			
			forval i = 1/5{
				g ls`i' = ret`i'5 - ret`i'1
				}
				
			* Regressions	
			
			forval i = 1(4)5{
				forval j = 1/5{
			
					ivreg2 ret`i'`j' MktRF HML SMB CMA RMW MOM, robust small bw(auto)
								
					* Store coefficients and standard errors
		
					mat B_FF6F = (B_FF6F \ (e(b)))
					matrix V = vecdiag(e(V))
					matmap V V, map(sqrt(@))
					matrix SE_FF6F = (SE_FF6F \ V)
					
					}
					
				ivreg2 ls`i' MktRF HML SMB CMA RMW MOM, robust small bw(auto)
								
				* Store coefficients and standard errors
		
				mat B_FF6F = (B_FF6F \ (e(b)))
				matrix V = vecdiag(e(V))
				matmap V V, map(sqrt(@))
				matrix SE_FF6F = (SE_FF6F \ V)	
					
				}
				
			restore	
				
			* Alphas
			
			preserve
	
			clear 
			svmat B_FF6F
			svmat SE_FF6F
	
			keep B_FF6F7 SE_FF6F7
			drop if _n==1
	
			ren B_FF6F7 alpha
			g t = alpha/SE_FF6F7
			g SE = SE_FF6F7
	
			* Reshape data
			
			g pf1 = 1
			replace pf1 = 5 if _n > 6
			g pf2 = mod(_n,6)
			replace pf2 = 6 if pf2==0
			
			keep pf1 pf2 alpha SE
			reshape wide alpha SE, i(pf1) j(pf2)

			* To string alphas
			
			foreach var of varlist alpha* {
				qui tostring `var', force replace format("%5.3f")
				}
			
			* Put t-stats below alpha
			
			tempfile tmp
			save `tmp'
			
			keep pf1 SE*
			forval i = 1/6{
				ren SE`i' alpha`i'
				qui tostring alpha`i', force replace format("%5.3f")
				qui replace alpha`i' = "(" + alpha`i' + ")"
				}
			append using `tmp'
			gsort pf1 -SE1
			
			* Changing lines
			
			tostring pf1, force replace
			replace pf1 = "" if mi(SE1)
			replace pf1 = "\addlinespace "  + pf1 if _n > 1
			drop SE*
			
			* Save data
			
			listtab * using "Results/Tables/04_09_`var2'_`var1'.tex", ///
				rstyle(tabular) replace ///
				head("\begin{tabular}{lcccccc}" ///
				"\toprule " ///
				" `var1' $\backslash$ $ iva $ & 1 & 2 & 3 & 4 & 5 & 5-1 \\" ///
				"\midrule ") ///
				foot("\bottomrule" "\end{tabular}")	
					
			restore
			
			* Restore coefficient matrices
			
			mat B_FF6F = J(1,7,.)
			mat SE_FF6F = J(1,7,.)
		
		}
	} 
	

